package com.heima.mq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Map;

@Component
public class SpringRabbitListener {
    @RabbitListener(queues = "work.queue")
    public void listenerQueueMessage(String message) throws InterruptedException {
        Thread.sleep(100);
        System.out.println("消费者1监听到:"+message+","+ LocalTime.now());
    }
    @RabbitListener(queues = "work.queue")
    public void listenerQueueMessage2(String message) throws InterruptedException {
        Thread.sleep(500);
        System.err.println("消费者2监听到:"+message+","+ LocalTime.now());
    }
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message){
        System.out.println("消费者1监听到 fanout.queue1:"+message+","+ LocalTime.now());
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message){
        System.out.println("消费者2监听到 fanout.queue2:"+message+","+ LocalTime.now());
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "hmall.direct"),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String message){
        System.out.println("消费者1监听到 direct.queue1:"+message+","+ LocalTime.now());
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "hmall.direct"),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String message){
        System.out.println("消费者2监听到 direct.queue1:"+message+","+ LocalTime.now());
    }
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String message){
        System.out.println("消费者1监听到 topic.queue1:"+message+","+ LocalTime.now());
    }
    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String message){
        System.out.println("消费者2监听到 topic.queue2:"+message+","+ LocalTime.now());
    }
    @RabbitListener(queues = "object.queue")
    public void listenTopicQueue2(Map<String,Object> message){
        System.out.println("消费者2监听到 topic.queue2:"+message+","+ LocalTime.now());
    }
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(Message message){
        System.out.println("消费者监听到 simple.queue:"+message.getMessageProperties().getMessageId()+","+ LocalTime.now());
        System.out.println("消费者监听到 simple.queue:"+new String(message.getBody())+","+ LocalTime.now());
        throw new RuntimeException("消费失败");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue"),
            exchange = @Exchange(name = "delay.direct",delayed = "true"),
            key = {"hi"}
    ))
    public void listenDelayQueue(String message){
        System.out.println("消费者监听到 delay.queue:"+message+","+ LocalTime.now());
    }


}
